//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Input;
using System.Diagnostics;

namespace OpenLS.Spreadsheet.UI
{
    static class GridPanes
    {
        internal static void EnableCommands(UIElement grid)
        {
            grid.CommandBindings.Add(new CommandBinding(WorksheetLayoutCommands.FreezePanes, OnFreezePanes, OnCanFreeze));
            grid.CommandBindings.Add(new CommandBinding(WorksheetLayoutCommands.UnfreezePanes, OnUnfreezePanes, OnCanUnfreeze));
            grid.CommandBindings.Add(new CommandBinding(WorksheetLayoutCommands.FreezeFirstColumn, OnFreezeFirstColumn, WorksheetHelper.OnAlways));
            grid.CommandBindings.Add(new CommandBinding(WorksheetLayoutCommands.FreezeTopRow, OnFreezeTopRow, WorksheetHelper.OnAlways));
            grid.CommandBindings.Add(new CommandBinding(WorksheetLayoutCommands.ToggleSplitPanes, OnToggleSplitPanes, WorksheetHelper.OnAlways));
        }
        static void OnCanFreeze(object sender, CanExecuteRoutedEventArgs args)
        {
            UIElement g = (WorksheetGrid)sender;
            Worksheet ws = SpreadsheetElement.GetWorksheet(g);
            if (ws == null)
                return;
            args.CanExecute = ws.View.Pane == null || ws.View.Pane.State == PaneState.Split;
            args.Handled = true;
        }
        static void OnCanUnfreeze(object sender, CanExecuteRoutedEventArgs args)
        {
            UIElement g = (WorksheetGrid)sender;
            Worksheet ws = SpreadsheetElement.GetWorksheet(g);
            if (ws == null)
                return;
            args.CanExecute = ws.View.Pane != null && ws.View.Pane.State != PaneState.Split;            
            args.Handled = true;
        }
        static void OnToggleSplitPanes(object sender, ExecutedRoutedEventArgs args)
        {
            UIElement g = (WorksheetGrid)sender;
            Worksheet ws = SpreadsheetElement.GetWorksheet(g);
            if (ws.View.Pane == null || ws.View.Pane.State == PaneState.Frozen)
            {
                SplitWorksheet(ws, PaneState.Split);
            }
            else
            {
                ws.SplitPanes(null, null);
            }

        }
        static void OnFreezeTopRow(object sender, ExecutedRoutedEventArgs args)
        {
            UIElement g = (WorksheetGrid)sender;
            Worksheet ws = SpreadsheetElement.GetWorksheet(g);

            ws.FreezePanes(1, null);
            g.InvalidateMeasure();
        }
        static void OnFreezeFirstColumn(object sender, ExecutedRoutedEventArgs args)
        {
            UIElement g = (WorksheetGrid)sender;
            Worksheet ws = SpreadsheetElement.GetWorksheet(g);
            
            ws.FreezePanes(null, 1);
            g.InvalidateMeasure();
        }
        static void OnFreezePanes(object sender, ExecutedRoutedEventArgs args)
        {
            Worksheet ws = SpreadsheetElement.GetWorksheet((DependencyObject)sender);
            SplitWorksheet(ws, PaneState.Frozen);
            args.Handled = true;
        }

        private static void SplitWorksheet(Worksheet ws, PaneState state)
        {
            int? row = ws.ActiveCell.Row.Index;
            if (row == 0)
                row = 5;

            int? column = ws.ActiveCell.Column.Index;
            if (column == 0)
                column = 5;
            if (ws.Selection is FullColumnRange)
                row = null;
            if (ws.Selection is FullRowRange)
                column = null;
            if (state == PaneState.Split)
                ws.SplitPanes(row, column);
            else
            {
                ws.FreezePanes(row, column);
            }
            if (state == PaneState.Split && false)
            {
                ws.View.Pane.State = PaneState.Split;
                ws.View.Pane.Width = ws.TopLeftPane.DesiredWidth.Value;
                ws.View.Pane.Height = ws.TopLeftPane.DesiredHeight.Value;
                foreach (WorksheetPane p in ws.GetPanes())
                {
                    p.NotifyPropertyChanged("DesiredWidth");
                    p.NotifyPropertyChanged("DesiredHeight");
                }
            }
        }

        static void OnUnfreezePanes(object sender, ExecutedRoutedEventArgs args)
        {
            Worksheet ws = SpreadsheetElement.GetWorksheet((DependencyObject)sender);
                ws.FreezePanes(null, null);
            args.Handled = true;
        }
        

    }
}
